package com.youkeyi.ddy.cloud.basic.pub.corp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.annex.entity.SysAnnex;
import com.youkeyi.ddy.cloud.basic.pub.corp.entity.SysCorp;
import com.youkeyi.ddy.cloud.basic.pub.corp.service.SysCorpService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 主体（麒麟、LAB）表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-02-19
 */
@Service
public class SysCorpController implements PublicCorpCloudService {

    @Autowired
    private SysCorpService sysCorpService;

    @Override
    public PublicCorpListCloudDto query(PublicCorpQueryCloudParam param) {
        PublicCorpListCloudDto dto = new PublicCorpListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCorpPrimaryKeys())) {
            param.getCorpPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNotInCorpPrimaryKeys())) {
            param.getNotInCorpPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNames())) {
            param.getNames().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getTypes())) {
            param.getTypes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCategories())) {
            param.getCategories().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCorp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCorpPrimaryKeys()), SysCorp::getPrimaryKey, param.getCorpPrimaryKeys())
                .notIn(CollectionUtils.isNotEmpty(param.getNotInCorpPrimaryKeys()), SysCorp::getPrimaryKey, param.getNotInCorpPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getNames()), SysCorp::getName, param.getNames())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysCorp::getName, param.getNameFuzzy())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), SysCorp::getCode, param.getCodes())
                .in(CollectionUtils.isNotEmpty(param.getTypes()), SysCorp::getType, param.getTypes())
                .in(CollectionUtils.isNotEmpty(param.getCategories()), SysCorp::getCategory, param.getCategories())
                .like(StringUtils.isNotBlank(param.getCodeFuzzy()), SysCorp::getCode, param.getCodeFuzzy())
                .like(StringUtils.isNotBlank(param.getUserNameFuzzy()), SysCorp::getUserName, param.getUserNameFuzzy())
                .like(StringUtils.isNotBlank(param.getUserPhoneFuzzy()), SysCorp::getUserPhone, param.getUserPhoneFuzzy())
                .ne(SysCorp::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;
        wrapper.lambda().orderByDesc(SysCorp::getId);

        Page<SysCorp> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCorp> corpList = sysCorpService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(corpList.getRecords())) {
            dto.setTotal(corpList.getTotal());

            List<PublicCorpListItemCloudDto> items = new ArrayList<>();
            corpList.getRecords().forEach(corp -> {
                PublicCorpListItemCloudDto item = new PublicCorpListItemCloudDto();
                item.setCorpPrimaryKey(corp.getPrimaryKey());
                item.setName(corp.getName());
                item.setCode(corp.getCode());
                item.setCategory(corp.getCategory());
                item.setType(corp.getType());
                item.setAuthType(corp.getAuthType());
                item.setEffectiveTime(corp.getEffectiveTime() != null ? corp.getEffectiveTime().getTime() : null);
                item.setExpiredTime(corp.getExpiredTime() != null ? corp.getExpiredTime().getTime() : null);
                item.setAppKey(corp.getAppKey());
                item.setAppSecret(corp.getAppSecret());
                item.setUserName(corp.getUserName());
                item.setUserPhone(corp.getUserPhone());
                item.setEmail(corp.getEmail());
                item.setBusinessLinceseName(corp.getBusinessLinceseName());
                item.setBusinessLicenseNo(corp.getBusinessLicenseNo());
                item.setLeftLogo(corp.getLeftLogo());
                item.setRightLogo(corp.getRightLogo());
                item.setManagementAttribution(corp.getManagementAttribution());
                item.setCoordinate(corp.getCoordinate());
                item.setIsDeleted(corp.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(corp.getCreateTime() != null ? corp.getCreateTime().getTime() : null);
                item.setCreateUserKey(corp.getCreateUserKey());
                item.setCreateUserName(corp.getCreateUserName());
                item.setUpdateTime(corp.getUpdateTime() != null ? corp.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(corp.getUpdateUserKey());
                item.setUpdateUserName(corp.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicCorpCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getName())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCorp corp = new SysCorp();
        corp.setPrimaryKey(SimpleUUidUtils.getUuid());
        corp.setName(param.getName());
        corp.setCode(StringUtils.isNotBlank(param.getCode()) ? param.getCode() : String.format("CORP-%s", System.currentTimeMillis()));
        corp.setCategory(param.getCategory());
        corp.setType(param.getType());
        corp.setAuthType(param.getAuthType());
        corp.setEffectiveTime(param.getEffectiveTime() != null ? new Date(param.getEffectiveTime()) : null);
        corp.setExpiredTime(param.getExpiredTime() != null ? new Date(param.getExpiredTime()) : null);
        corp.setAppKey(param.getAppKey());
        corp.setAppSecret(param.getAppSecret());
        corp.setUserName(param.getUserName());
        corp.setUserPhone(param.getUserPhone());
        corp.setEmail(param.getEmail());
        corp.setBusinessLinceseName(param.getBusinessLinceseName());
        corp.setBusinessLicenseNo(param.getBusinessLicenseNo());
        corp.setLeftLogo(param.getLeftLogo());
        corp.setRightLogo(param.getRightLogo());
        corp.setManagementAttribution(param.getManagementAttribution());
        corp.setCoordinate(param.getCoordinate());
        corp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        corp.setCreateUserKey(param.getCreateUserKey());
        corp.setCreateUserName(param.getCreateUserName());
        corp.setCreateTime(new Date());
        boolean save = sysCorpService.save(corp);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(corp.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicCorpUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCorp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCorp::getPrimaryKey, param.getCorpPrimaryKey());
        SysCorp corp = new SysCorp();
        corp.setPrimaryKey(param.getCorpPrimaryKey());
        corp.setName(param.getName());
        corp.setCode(param.getCode());
        corp.setCategory(param.getCategory());
        corp.setType(param.getType());
        corp.setAuthType(param.getAuthType());
        corp.setEffectiveTime(param.getEffectiveTime() != null ? new Date(param.getEffectiveTime()) : null);
        corp.setExpiredTime(param.getExpiredTime() != null ? new Date(param.getExpiredTime()) : null);
        corp.setAppKey(param.getAppKey());
        corp.setAppSecret(param.getAppSecret());
        corp.setUserName(param.getUserName());
        corp.setUserPhone(param.getUserPhone());
        corp.setEmail(param.getEmail());
        corp.setBusinessLinceseName(param.getBusinessLinceseName());
        corp.setBusinessLicenseNo(param.getBusinessLicenseNo());
        corp.setLeftLogo(param.getLeftLogo());
        corp.setRightLogo(param.getRightLogo());
        corp.setManagementAttribution(param.getManagementAttribution());
        corp.setCoordinate(param.getCoordinate());
        corp.setUpdateUserKey(param.getUpdateUserKey());
        corp.setUpdateUserName(param.getUpdateUserName());
        corp.setUpdateTime(new Date());
        boolean save = sysCorpService.update(corp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicCorpDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCorpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCorp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCorp::getPrimaryKey, param.getCorpPrimaryKeys());

        SysCorp corp = new SysCorp();
        corp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        corp.setUpdateUserKey(param.getUpdateUserKey());
        corp.setUpdateUserName(param.getUpdateUserName());
        corp.setUpdateTime(new Date());
        boolean save = sysCorpService.update(corp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
